package com.iot.inter.interfaceprocessing.newinterface;


import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.TurntoInterfaceUtils;
import com.iot.inter.entity.newpojo.request.RequestBody.QueryPartnerRequestBody;
import com.iot.inter.entity.newpojo.request.RequestHeader;
import com.iot.inter.entity.newpojo.request.subRequest.QueryPartnerRequest;
import com.iot.inter.entity.newpojo.response.subResponse.QueryPartnerResponse;
import com.iot.inter.utils.HttpClientTool;
import com.iot.inter.utils.JaxbUtil;
import org.apache.log4j.Logger;

/**
 * Demo 去北京查询卡片所属的企业
 *
 * @author lm
 * @date 2018/08/31
 */
public class TurnToQueryPartnerService {

    private static Logger logger = Logger.getLogger(TurnToQueryPartnerService.class);

    public OrderPaymentManager queryPartner(String deviceId) {
        try {
            //请求参数处理
            QueryPartnerRequestBody body = new QueryPartnerRequestBody();
            body.setDeviceId(deviceId);
            // 请求参数拼接
            QueryPartnerRequest request = new QueryPartnerRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_queryPartnerByDeviceId, Constant.cmd_accessCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), Constant.cmd_secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(QueryPartnerRequest.class).toXml(request, "utf-8");
            logger.info("【根据ICCID查询PartnerCode】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_queryPartnerByDeviceId + ";ICCID:" + deviceId);

            logger.info("【根据ICCID查询PartnerCode】xml响应：" + result);
            QueryPartnerResponse responseObj = new JaxbUtil(QueryPartnerResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                //成功，拿到回传的结果回去处理
                OrderPaymentManager user = new OrderPaymentManager();
                user.setPartnerCode(responseObj.getBody().getPartnerCode());
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;
            } else {
                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }
}
